package com.doule.farmerassistant.plan;

import java.util.ArrayList;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.doule.farmerassistant.R;
import com.doule.farmerassistant.task.TaskDetail;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CreatePlanActivity extends Activity implements OnClickListener {
    private CreatePlanActivity mCreatePlanActivity;
    private ArrayList<TaskDetail> temporaryList;
    private TextView mShowTasksTV, mShowDateTV;
    private EditText mInputNameED;
    private String mAccount, mToken;

    public void showTasks(ArrayList<TaskDetail> temporaryList) {
        this.temporaryList = temporaryList;
        String taskList = "";
        if (temporaryList.size() != 0) {
            for (TaskDetail data : temporaryList) {
                taskList = data.getName() + ";  " + taskList;
            }
        }
        mShowTasksTV.setVisibility(View.VISIBLE);
        mShowTasksTV.setText(taskList);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_plan);
        findViewById(R.id.btn_add_plan).setOnClickListener(this);
        findViewById(R.id.btn_confirm_plan).setOnClickListener(this);
        findViewById(R.id.btn_set_date).setOnClickListener(this);
        mShowTasksTV = (TextView) findViewById(R.id.tv_show_tasks_list);
        mInputNameED = (EditText) findViewById(R.id.ed_inpu_plan_name);
        mShowDateTV = (TextView) findViewById(R.id.tv_show_plan_date);
        mAccount = getIntent().getStringExtra("account");
        mToken = getIntent().getStringExtra("tk");
        mCreatePlanActivity = this;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.btn_add_plan) {
            new ShowTaskDialog(mCreatePlanActivity);
        } else if (v.getId() == R.id.btn_confirm_plan) {
            String planName = mInputNameED.getText().toString();
            if (temporaryList == null || temporaryList.size() < 1) {
                Toast.makeText(
                        mCreatePlanActivity,
                        mCreatePlanActivity.getString(R.string.please_add_task),
                        Toast.LENGTH_LONG).show();
            } else if (planName == null || planName.equals("")) {
                Toast.makeText(
                        mCreatePlanActivity,
                        mCreatePlanActivity.getString(R.string.plan_name_error),
                        Toast.LENGTH_LONG).show();
            } else {
                String planID = getUUID();
                // String[] taskIds = new String[temporaryList.size()];
                JSONArray jaId = new JSONArray();
                JSONArray jaName = new JSONArray();
                for (int i = 0; i < temporaryList.size(); i++) {
                    String id = temporaryList.get(i).getId();
                    String name = temporaryList.get(i).getName();
                    jaId.put(id);
                    jaName.put(name);
                }
                setJSON(planID, planName, jaId, jaName);
            }
        } else if (v.getId() == R.id.btn_set_date) {
            new SetDateDialog(mCreatePlanActivity);
        }
    }

    private String getUUID() {
        UUID uuid = UUID.randomUUID();
        String uu = uuid.toString().replace("-", "");
        return uu;
    }

    private int mDayType = 0, mDay = 0, mStartTime = 0;

    private void initInformation() {
        mDayType = 0;
        mDay = 0;
        mStartTime = 0;
        mInputNameED.setText("");
        temporaryList = null;
        mShowTasksTV.setVisibility(View.GONE);
        mShowDateTV.setVisibility(View.GONE);
    }

    /**
     * Set date.
     * @param dayType
     * Day type
     * @param day
     * @param startTime
     * @param dayS
     * @param timeS
     */
    public void setDateInformation(int dayType, int day, int startTime,
            String dayS, String timeS) {
        mDayType = dayType;
        mDay = day;
        mStartTime = startTime;
        String string = "";
        if (dayType == 1) {
            string = mCreatePlanActivity.getString(R.string.rb_everyday)
                    + "   " + timeS;
        } else if (dayType == 2) {
            string = timeS;
        } else if (dayType == 3) {
            string = dayS + "   " + timeS;
        }
        mShowDateTV.setText(string);
        mShowDateTV.setVisibility(View.VISIBLE);
    }

    /**
     * Setting Json parameter.
     * 
     * @param planId
     *            Plan id.
     * @param planName
     *            Plan name.
     * @param taskIds
     *            Task ids belong this plan.
     * @param jaName
     *            Json array.
     */
    private void setJSON(String planId, String planName, JSONArray taskIds,
            JSONArray jaName) {
        if (mDayType == 0 || mStartTime == 0) {
            Toast.makeText(mCreatePlanActivity,
                    mCreatePlanActivity.getString(R.string.please_select_date),
                    Toast.LENGTH_LONG).show();
        } else {
            JSONObject js = new JSONObject();
            try {
                js.put("PlanId", planId);
                js.put("PlanName", planName);
                js.put("Account", mAccount);
                js.put("TaskIds", taskIds);
                js.put("StartTime", mStartTime);
                js.put("Day", mDay);
                js.put("DayType", mDayType);
                js.put("State", 0);
                js.put("TaskNames", jaName);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            initInformation();
            CreatePlanTask g = new CreatePlanTask(mCreatePlanActivity, mToken,
                    js.toString());
            g.go();
        }
    }
}
